#include "contact.h"


// 封装一个菜单显示界面的函数
void ShowMenu()
{
    cout << "========================" << endl;
    cout << "===== 1.添加联系人 =====" << endl;
    cout << "===== 2.显示联系人 =====" << endl;
    cout << "===== 3.删除联系人 =====" << endl;
    cout << "===== 4.查找联系人 =====" << endl;
    cout << "===== 5.修改联系人 =====" << endl;
    cout << "===== 6.清空联系人 =====" << endl;
    cout << "===== 0.退出通讯录 =====" << endl;
    cout << "========================" << endl;
}
// 添加联系人
void AddPerson(struct AddressBooks* contact)
{
    // 判断通讯录是否已满
    if (contact->m_size == MAX)
    {
        cout << "通讯录已满, 无法添加新的联系人" << endl;
        return;
    }
    else
    {
        //添加具体的联系人
        // 姓名:
        cout << "请输入姓名: ";
        cin >> contact->personarray[contact->m_size].m_name;
        // 性别:
        cout << "请输入性别(1--男; 2--女): ";
        int sex = 0;
        while (1)
        {
            cin >> sex;
            if (sex == 1 || sex == 2)
            {
                contact->personarray[contact->m_size].m_sex = sex;
                break;
            }
            cout << "性别输入错误, 请重新输入: ";
        }
        // 电话号码:
        cout << "请输入电话号码: ";
        cin >> contact->personarray[contact->m_size].m_phone;

        // 更新通讯录联系人数量
        contact->m_size++;
        cout << "添加成功" << endl;

        //system("pause");//暂停屏幕，任意键之后清屏
        //system("cls");
    }
}


// 显示所有联系人
void ShowPerson(struct AddressBooks* contact)
{
    //判断通讯录中人数是否为0
    if (contact->m_size == 0)
    {
        cout << "当前通讯录为空" << endl;
    }
    else
    {
        cout << "姓名\t" << "性别\t" << "电话号码" << endl;
        for (int i=0; i < contact->m_size; i++)
        {
            cout << contact->personarray[i].m_name << "\t"
                << (contact->personarray[i].m_sex == 1? "男":"女") << "\t"
                << contact->personarray[i].m_phone << endl;
        }
    }
    //system("pause");//独立窗口时使用
    //system("cls");
}

// 检测特定联系人是否存在
int IsExist(struct AddressBooks* contact, string name)
{
    //如果存在则返回联系人的位置
    //不存在则返回-1
    for (int i = 0; i < contact->m_size; i++)
    {
        if (contact->personarray[i].m_name == name)
        {
            return i;//找到就返回联系人所在的位置
        }
    }
    return -1;//遍历结束没有找到返回-1
}

// 删除指定联系人
void DeletePerson(struct AddressBooks* contact)
{
    cout << "请输入您要删除的联系人姓名: ";
    string name;
    cin >> name;
    int ret = IsExist(contact, name);
    if (ret != -1)
    {
        for (int i = ret; i < contact->m_size; i++)
        {
            //数据前移
            contact->personarray[i] = contact->personarray[i+1];
        }
        contact->m_size--;
        cout << "删除成功" << endl;
    }
    else
    {
        cout << "查无此人" << endl;
    }
    //system("pause");
    //system("cls");

}

// 按照姓名查找指定联系人
void FindPerson(struct AddressBooks* contact)
{
    cout << "请输入您要查找的联系人: ";
    string name;
    cin >> name;
    //判断指定的联系人是否存在通讯录中
    int ret = IsExist(contact, name);
    if (ret != -1)
    {
        cout << "姓名\t" << "性别\t" << "电话号码" << endl;
        cout << contact->personarray[ret].m_name << "\t"
                << (contact->personarray[ret].m_sex == 1? "男":"女") << "\t"
                << contact->personarray[ret].m_phone << endl;
    }
    else
    {
        cout << "查无此人" << endl;
    }
    //system("pause");
    //system("cls");
}

void ModifyPerson(struct AddressBooks* contact)
{
    cout << "请输入您要修改的联系人: ";
    string name;
    cin >> name;
    int  ret = IsExist(contact, name);
    if (ret != -1)
    {
        cout << "请输入姓名: ";
        cin >> contact->personarray[ret].m_name;
        // 性别:
        cout << "请输入性别(1--男; 2--女): ";
        int sex = 0;
        while (1)
        {
            cin >> sex;
            if (sex == 1 || sex == 2)
            {
                contact->personarray[ret].m_sex = sex;
                break;
            }
            cout << "性别输入错误, 请重新输入: ";
        }
        // 电话号码:
        cout << "请输入电话号码: ";
        cin >> contact->personarray[ret].m_phone;
        cout << "修改成功" << endl;
    }
    else
    {
        cout << "查无此人" << endl;
    }
    //system("pause");
    //system('cls');
}


void CleanPerson(struct AddressBooks* contact)
{
    cout << "确认删除吗, 此操作不可撤回, 确认请输入1, 否则输入0: ";
    int querry = 0;
    while(1)
    {
        cin >> querry;
        if (querry == 1)
        {
            contact->m_size = 0; //将当前记录联系人数量置为0，做逻辑清空操作
            cout << "通讯录已清空" << endl;
            break;
        }
        else if (querry == 0)
        {
            cout << "您已取消清空通讯录" << endl;
            break;
        }
        else
        {
            cout << "选项不存在, 请按要求输入(确认输入1, 否则输入0): ";
        }
    }
    //system("pause");
    //system("cls");
}